// 获取客服列表
function getCustomList(callback) {
    $.ajax({
        dataType: "JSON",
        type: "post",
        url: '/admin/Custm_svc/getCustomerServiceInfo.html',
        success: function (res) {
            callback(res)
        }
    })
}

//获取广告二维码列表
function getAdvList(callback){
    $.ajax({
        url:'/admin/Qrcode/getQrList.html',
        success : function (res) {
            callback(res);
        }
    });
}

//获取父级客服列表
function getPersonCsList(callback){
    $.ajax({
        url:'/admin/Custm_svc/getPersonCustom.html',
        success : function (res) {
            callback(res);
        }
    });
}


// 提交添加客服的信息
function SubmitCustomList(aa) {
    $.ajax({
        dataType: 'JSON',
        type: 'post',
        url: '/admin/Custm_svc/addCust.html',
        data: aa,
        success: function (res) {
            console.log(res)
            if (res.errmsg == 'ok') {
                layer.msg('添加成功');
                setTimeout(function () {
                    window.location.reload();
                }, 1000);
            } else {
                layer.msg('添加失败，请检查');
            }

        }
    })
}

// 提交添加客服的信息
$("#sumbitCustomList").click(function () {
    var that = this;
    var formMassage = $(that).parent().parent("form").serializeArray();
    var customListMessage = {};
    formMassage.forEach(function (item) {
        customListMessage[item.name] = item.value.split("@")[0]
    })
    customListMessage.img_path = $('.customImg').attr('src')
    SubmitCustomList(customListMessage)
})

// 邀请客服绑定微信号
function visitCustomWechat(account, wechat) {
    $.ajax({
        dataType: 'JSON',
        type: 'post',
        url: '/admin/Custm_svc/InviteBindCust',
        data: {
            kf_account: account,
            wechat_num: wechat
        },
        success: function (res) {
            console.log(res);
            if (res.errmsg == 'ok') {
                layer.msg('邀请成功');
                setTimeout(function () {
                    window.location.reload();
                }, 1000);
            } else {
                layer.msg('邀请失败，请重试');
                setTimeout(function () {
                    window.location.reload();
                }, 1000);
            }
        }
    })
}
// 邀请客服绑定微信号
$("#sumbitCustomWechat").click(function () {
    var account = $("#inviteCustomAccount").val().split("@")[0];
    var wehat_num = $("#inviteCustomWechat").val();
    console.log($("#inviteCustomAccount").val().split("@")[0]);
    visitCustomWechat(account, wehat_num);
})

// 取消绑定微信号
$("#resetButton").click(function(){
    $("#invitePopup").fadeOut();
})

// 修改在线客服信息
function editCustomMessages(obj){
    $.ajax({
        dataType: 'JSON',
        type: 'post',
        url: '/admin/Custm_svc/updateCust',
        data: obj,
        success: function(res){
            if (res.errmsg == 'ok') {
                layer.msg('修改成功');
                setTimeout(function () {
                    window.location.reload();
                }, 1000);
            } else {
                layer.msg('修改失败，请重试');
                setTimeout(function () {
                    window.location.reload();
                }, 1000);
            }
        }
    })
}

$("#sureEditCustomMessage").click(function(){
    var aa = {};
    aa.username = $("#editCustomAccount").val().split("@")[0];
    aa.nickname = $("#editCustomNick").val();
    aa.img_path = $('.editCustomImg').attr('src') == undefined ? '' : $('.editCustomImg').attr('src');
    aa.old_password = $('#editCustomOldPwd').val();
    aa.password = $('#editCustomNewPwd').val();
    aa.confirm_password = $('#editCustomCfrPwd').val();
    editCustomMessages(aa)
})

$("#cancelEdit").click(function(){
    $("#editCustomMessage").fadeOut();
})


// 删除客服
function deleteCustomMessage(bbb){
    $.ajax({
        dataType: 'JSON',
        type: 'post',
        url: '/admin/Custm_svc/deleteCust?kf_account=' + bbb,
        data: '',
        success: function(res){
            if (res.errmsg == 'ok') {
                layer.msg('删除成功');
                setTimeout(function () {
                    window.location.reload();
                }, 1000);
            } else {
                layer.msg('删除失败，请重试');
                setTimeout(function () {
                    window.location.reload();
                }, 1000);
            }
        }
    })
}
    
// 获取在线客服客服列表
function getOnlineCustom(callback) {
    $.ajax({
        dataType: 'JSON',
        type: 'post',
        url: '/admin/Custm_svc/queryOnlineCust',
        data: '',
        success: function (res) {
            callback(res)
        }
    })
}

$(document).ready(function () {
    layui.use('upload', function () {
        layui.upload({
            url: '/api/upload/uploadwxheadimg',
            before: function (input) {
                //返回的参数item，即为当前的input DOM对象
                console.log('文件上传中');
            },
            success: function (res) {
                console.log(res.url);
                $(".customImg").attr('src',res.url)
                $(".editCustomImg").attr('src',res.url)

            }
        });
    });
})


// 分页函数
function setPage(el, data, callback){
    // console.log(data)
    window[el] = new iPage({
        elem: '#'+el,
        limit: 10, //分页数
        curr: 1, //初始页
        count: data.length, //数据总数,
        hidenum: 10,
        topages: true,
        jump: function(obj, first){
            callback(obj, first)
            // console.log(obj)
        }
    });
}

//初始化密码
function iniPassword(){
    $.ajax({
        url:"/admin/custm_svc/initServerPassword",
        success:function(res){
            $("#showConfirm").fadeOut(50);
            layer.msg(res.msg);
        }
    });
}

//取消初始化密码操作
function cancelControllerPassword(){
    $("#showConfirm").fadeOut(50);
}

//获取客服历史信息
function getHistoryMsg(callback){
    $.ajax({
        url:"/admin/custm_svc/syncChatMsg",
        success:function(res){
            callback(res);
        }
    });
}

//保存离线回复消息
function saveReplyMsg(id,data,callback){
    console.log(data);
    console.log(id);
    $.ajax({
        url:"/admin/custm_svc/saveReply/id/" + id,
        type:"post",
        data:data,
        success:function(res){
            callback(res);
        }
    });
}

//设置备注
function setCsRemark(id , content,callback){
    $.ajax({
        url:"/admin/custm_svc/setRemarkMsg",
        type:"POST",
        data:{id:id,remark:content},
        success:function(res){
            callback(res);
        }
    });
}

//保存推广海报
function savePoster(id,data,callback){
    $.ajax({
        url:"/admin/custm_svc/savePoster/id/" + id,
        type:"post",
        data:data,
        success:function(res){
            callback(res);
        }
    });
}

